package cn.org.donald.framework.handler;

import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.pojo.vo.HttpResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author ： Donald
 * @date ： 2020/10/16 23:50
 * @description： 全局异常拦截
 */
@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);






    /**
     * 捕捉自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseBody
    public HttpResult serviceException(ServiceException e){
        logger.info("正在捕抓异常:{}",e.getClass()+"");
        return new HttpResult(e.getCode(), e.getInfo());
    }

    /**
     * 权限校验不通过
     * @param e
     * @return
     */
    @ExceptionHandler(UnauthorizedException.class)
    @ResponseBody
    public HttpResult unauthorizedException(UnauthorizedException e){
        logger.info("正在捕抓异常:{}",e.getClass()+"");
        return new HttpResult(HttpStatus.FORBIDDEN.value(), InfoConstant.FORBIDDEN);
    }
    /**
     * 身份认证失败
     * @param e
     * @return
     */
    @ExceptionHandler(UnauthenticatedException.class)
    @ResponseBody
    public HttpResult unauthenticatedException(UnauthenticatedException e){
        logger.info("正在捕抓异常:{}",e.getClass()+"");
        return new HttpResult(HttpStatus.UNAUTHORIZED.value(), InfoConstant.UNAUTHORIZED);
    }
    /**
     * 用户名不存在异常
     * @param e
     * @return
     */
    @ExceptionHandler(UnknownAccountException.class)
    @ResponseBody
    public HttpResult unknownAccountException(UnknownAccountException e){
        logger.info("正在捕抓异常:{}",e.getClass()+"");
        String message = e.getMessage();
        return new HttpResult(HttpStatus.UNAUTHORIZED.value(),message);
    }

    /**
     * jwt验证异常
     * @param e
     * @return
     */
    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public HttpResult authenticationException(AuthenticationException e){
        logger.info("正在捕抓异常:{}",e.getClass()+"");
        String message = e.getMessage();
        return new HttpResult(HttpStatus.UNAUTHORIZED.value(),message);
    }
    /**
     * 用户密码错误异常
     * @param e
     * @return
     */
    @ExceptionHandler(IncorrectCredentialsException.class)
    @ResponseBody
    public HttpResult incorrectCredentialsException(IncorrectCredentialsException e){
        logger.info("正在捕抓异常:{}",e.getClass()+"");
        String message = e.getMessage();
        return new HttpResult(HttpStatus.UNAUTHORIZED.value(),message);
    }


    /**
     * 空指针异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public HttpResult nullPointerException(Exception e){
        e.printStackTrace();
        logger.info("正在捕抓异常:{}",e.getClass()+"");
        return new HttpResult(HttpStatus.INTERNAL_SERVER_ERROR.value(),InfoConstant.NULL_POINTER_EXCEPTION);
    }

    /**
     * 全局异常处理
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public HttpResult exception(Exception e){
        e.printStackTrace();
        logger.info("正在捕抓异常:{}",e.getClass()+"");
        return new HttpResult(HttpStatus.INTERNAL_SERVER_ERROR.value(),InfoConstant.INTERNAL_SERVER_ERROR);
    }

}
